package de.baumann.browser.activitys.duobao

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.util.Log
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import de.baumann.browser.R
import de.baumann.browser.api.net.vo.duobao.AddressInfoPO
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.duobao.IAddressView
import de.baumann.browser.present.duobao.AddressPresenter
import kotlinx.android.synthetic.main.activity_address.*
import kotlinx.android.synthetic.main.include_tool_bar_2.*

class AddressActivity : BaseOdinActivity<IAddressView, AddressPresenter>(), IAddressView {

    companion object {
        fun startAddressActivity(context: Context) {
            context.startActivity(Intent(context, AddressActivity::class.java))
        }
    }

    private  var t = 0

    override fun getLayout(): Int {
        return R.layout.activity_address
    }

    override fun getTitleText(): String {
        return "收货地址"
    }

    override fun getBackIcon(): Int {
        return R.drawable.ic_back
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        shrdz.setOnClickListener {
            presenter?.initAddressPicker(mContext)
        }

        shrdh.setOnClickListener {
            DuoBaoPhoneActivity.startDuoBaoPhoneActivity(this)
        }

        btndzbc.setOnClickListener {
            if (t==0){
                presenter?.duobaotjdz()
            }else{
                presenter?.duobaogxdz()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && data != null) {
            shrdh.text = data.getStringExtra("phone")
        }
    }

    override fun initData() {
        presenter?.getSHDZ()
    }

    override fun createPresenter(): AddressPresenter {
        return AddressPresenter()
    }

    override fun createView(): IAddressView {
        return this
    }

    override val n: String
        get() = shrxm.text.toString().trim()
    override val p: String
        get() = shrdh.text.toString().trim()
    override val a: String
        get() = shrdz.text.toString().trim()
    override val i: String
        get() = xxdz.text.toString().trim()

    override fun setName(name: String) {
        t = 1
        shrxm.setText(name)
    }

    override fun setPhone(phone: String) {
        t = 1
        shrdh.text = phone
    }

    override fun setAddress(address: String) {
        t = 1
        shrdz.text = address
    }

    override fun setAddInfo(info: String) {
        t = 1
        xxdz.setText(info)
    }

    private var address = ""

    override fun showAddressPicker(provinceItems: MutableList<AddressInfoPO>, cityItems: MutableList<MutableList<AddressInfoPO>>, areaItems: MutableList<MutableList<MutableList<AddressInfoPO>>>) {
        val addressPv =
                OptionsPickerBuilder(this, OnOptionsSelectListener { options1, options2, options3, _ ->
                    //省份
                    val province = provinceItems[options1].name
                    //城市
                    val city = cityItems[options1][options2].name
                    //辖区
                    val area = areaItems[options1][options2][options3].name
                    address = "$province $city $area"
                    shrdz.text = address
                })
                        .setTitleText("地址选择")
                        .setDividerColor(Color.BLACK)
                        .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                        .setContentTextSize(20)
                        .build<AddressInfoPO>()
        addressPv.setPicker(provinceItems, cityItems, areaItems)
        addressPv.show()
    }

    override fun dztjcg() {
        showShortToast("保存成功")
    }

}